const {Sequelize,DataTypes} = require('sequelize');

//4.实例化Sequelize，完成链接配置


const sequelize = new Sequelize('traveldb', 'root', '12345678', {
    //连接选项
    host: 'localhost', // 数据库地址
    dialect: 'mysql', // 指定连接的数据库类型
    dialectOptions: { charset: "utf8mb4" },
    'define': {
      'underscored': true,//字段下划线
      'charset': 'utf8mb4',//必须设置了才能保存中文
      'freezeTableName': true//所有表将使用与模型名称相同的名称
    },
    pool: {
      max: 5, // 连接池的最大连接数量
      min: 0, // 连接池的最小连接数量
      idle: 10000 // 如果一个线程10秒钟内没有被使用，那么就释放线程
    }
  });

  const UserModel = require('./model/user')(sequelize,DataTypes);
  const ArticleModel = require('./model/article')(sequelize,DataTypes);
  const TravelModel = require('./model/travel')(sequelize,DataTypes);
  const OrderModel = require('./model/order')(sequelize,DataTypes);
  
   //写关系
  //一对多
  UserModel.hasMany(ArticleModel,{foreignKey:'userId'});
  TravelModel.hasMany(ArticleModel,{foreignKey:'travelId'});
  TravelModel.hasMany(OrderModel,{foreignKey:'travelId'});
  //多对多
/*   TravelModel.belongsToMany(OrderModel, { through: "TravelOrder", foreignKey: "travelId" });
  OrderModel.belongsToMany(TravelModel, { through: "TravelOrder", foreignKey: "orderId" }); */

  
  module.exports = {
    sequelize,
    UserModel,
    ArticleModel,
    TravelModel,
    OrderModel
  }
